package com.active.web;

import com.active.dao.entity.MyBean;
import com.active.service.AjaxPageResult;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/6/13 0013.
 */
@RestController
@RequestMapping("processEx")
@Slf4j
public class ProcessExController {

    @Resource(name = "taskService")
    private TaskService taskService;

    @Resource(name = "runtimeService")
    private RuntimeService runtimeService;

    @Resource(name = "repositoryService")
    private RepositoryService repositoryService;

    @Resource(name = "identityService")
    private IdentityService identityService;

    @Resource(name = "historyService")
    private HistoryService historyService;

    @Resource(name = "formService")
    private FormService formService;

    @RequestMapping("testEx")
    public AjaxPageResult testEx(){
        MyBean myBean = new MyBean();

        Map<String,Object> map = new HashMap<>();
        map.put("myBean",myBean);
        map.put("name","liyunlong");
        identityService.setAuthenticatedUserId("liyunlong");//图中的authenticatedUserId单词写错了，导致获取不了启动人id，懒得修改了
        String businessKey = "99999";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("processEx",businessKey,map);
        String oneName = (String) runtimeService.getVariable(processInstance.getId(),"authenticatedUserIdForTest");

        String second = (String) runtimeService.getVariable(processInstance.getId(),"returnValue");

        String businessKey3 = (String) runtimeService.getVariable(processInstance.getId(),"businessKey");

        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();

        String setByTask = (String) taskService.getVariable(task.getId(),"setByTask");

        return AjaxPageResult.success("one:"+oneName+",second:"+second+",businessKey3:"+businessKey3+",setByTask:"+setByTask);

    }


}
